//
//  ChangeBuTaMenController.m
//  FengyuanXS
//
//  Created by 王冬 on 2020/7/27.
//  Copyright © 2020 王冬. All rights reserved.
//

#import "ChangeBuTaMenController.h"
#import "ClassViewController.h"
#import "LoginViewController.h"
#import "RootViewController.h"
@interface ChangeBuTaMenController ()
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *companyLabel;
@property (weak, nonatomic) IBOutlet UILabel *sectionLabel;
@property (weak, nonatomic) IBOutlet UITextField *xinCompanyLabel;
@property (weak, nonatomic) IBOutlet UITextField *xinSectionLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UITextField *codeText;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;
- (IBAction)codeBtn:(UIButton *)sender;

@property (nonatomic, copy) NSString *sectionID;
@property (nonatomic, copy) NSString *companyID;

@end

@implementation ChangeBuTaMenController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"修改部门";
    
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self getUserData];
}

#pragma mark - Table view data source


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 3) {
        
        ClassViewController *testVC = [ClassViewController loadStroyboard:[ClassViewController new]];
        WS(weakSelf);
        testVC.claasName = ^(NSString * _Nonnull name, NSString * _Nonnull classID, NSString * _Nonnull companyID, NSString * _Nonnull company) {
            weakSelf.sectionID = classID;
            weakSelf.companyID = companyID;
            weakSelf.xinSectionLabel.text = name;
            weakSelf.xinCompanyLabel.text = company;
        };
        
        
        STPopupController *popupController = [[STPopupController alloc] initWithRootViewController:testVC];
        popupController.style = STPopupStyleFormSheet;
        popupController.transitionStyle = STPopupTransitionStyleFade;
        popupController.containerView.layer.cornerRadius = 5.0f;
        [popupController setNavigationBarHidden:YES];
        [popupController presentInViewController:self];
    }
}


-(void)getUserData{
    WS(weakSelf);
    [[ToastManager shardInstance]showSystemProgress:self.view];
    [[ApiManager ShardInstance]userinfoResultBlock:^(NSDictionary *data, NSError *error) {
        UserModel *model = [UserModel mj_objectWithKeyValues:data[@"rs"]];
        [[ToastManager shardInstance]hideProgress:self.view];
        weakSelf.nameLabel.text = model.real_name;
        weakSelf.companyLabel.text = model.company_name;
        weakSelf.sectionLabel.text = model.section_name;
        weakSelf.phoneLabel.text = [TokenManager getToken];
        
    }];
}


- (IBAction)codeBtn:(UIButton *)sender {
    [[ApiManager ShardInstance]codeuserID:[TokenManager getUserId] mobile:self.phoneLabel.text type:@"0" resultBlock:^(NSDictionary *data, NSError *error) {
           BaseModel *model = [BaseModel mj_objectWithKeyValues:data];
           if (model.code == 1) {
               [AuthCodeManager getValidCode:sender];
               [[ToastManager shardInstance]showToast:model.msg];
           }
           
       }];
    
}


- (IBAction)commitBtn:(BoardButton *)sender {
    
    if (self.xinCompanyLabel.text.length <=0 ||self.xinSectionLabel.text.length <= 0) {
        [[ToastManager shardInstance]showToast:@"请选择新的公司和部门"];
        return;
    }else{
        
        [[ApiManager ShardInstance]editSectionid:[TokenManager getUserId] company_id:self.companyID section_id:self.sectionID captcha:self.codeText.text resultBlock:^(NSDictionary *data, NSError *error) {
            BaseModel *model = [BaseModel mj_objectWithKeyValues:data];
            
            if (model.code ==1) {
                [[ToastManager shardInstance]showToast:@"修改成功"];
             //   [self.navigationController popViewControllerAnimated:YES];
                [TokenManager removeUserId];
                [TokenManager removeToken];
                [self quitApp];

                
            }
            
        }];
        
    }
    
    
}

-(void)quitApp{
    
    
    
    LoginViewController *loginVC = [LoginViewController loadStroyboard:[LoginViewController new]];
    
    UINavigationController * nav = [[RootViewController alloc] initWithRootViewController:loginVC];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:nav animated:YES completion:nil];
    
}


@end
